//index.js
//获取应用实例
const app = getApp();
let utils = require('../../utils/util.js');

Page({
  data: {
    topIcon: null,
    cellLoading: false,       //是否
    allItemsLoaded: false,
    scrollHeight: 0,        //"滚动视图的高度"
    curPage: 1,         //当前页
    pageNum: 3,         //每页多少条商品数据
    totalPage: 100,
    goodsItems: [],
    firstShow: true,
    //获得dialog组件
    dialog:null
  },
  requestData(fisrtLoad = false) {
    app.post('goods/item/list', {
      pageNum: this.data.curPage,
      pageSize: this.data.pageNum
    }).then((res) => {
      let resultObj = res.data.data;
      this.data.totalPage = resultObj.pageCount;
      if (this.data.curPage > this.data.totalPage) {
        this.setData({ cellLoading: true });
        this.setData({ allItemsLoaded: true });
        return;
      }
      let serverItems = resultObj.list;
      let goodsItems = this.data.goodsItems;
      if (this.data.curPage == this.data.totalPage) {    //所有数据加载完了
        this.setData({ cellLoading: true });
        goodsItems.push(...serverItems);
        this.setData({ "goodsItems": goodsItems, allItemsLoaded: true });
      }
      else {  //满一页
        this.setData({ cellLoading: false });
        this.setData({ curPage: this.data.curPage + 1 });
        goodsItems.push(...serverItems);
        this.setData({ "goodsItems": goodsItems });
      }
    }).catch((errMsg) => {
      console.log(errMsg);    //错误提示信息
    });
  },
  checkRefreshData() {
    app.post('goods/item/list', {
      pageNum: 1,
      pageSize: 100
    }).then((res) => {
      let resultObj = res.data.data;
      let serverItems = resultObj.list;
      let hasChange = false;
      for (let i = 0; i < this.data.goodsItems.length; i++) {
        let goodItem = this.data.goodsItems[i];
        let checkItem = serverItems[i];
        if (goodItem.itemId != checkItem.itemId) {
          hasChange = true;
          break;
        }
      }
      if (!hasChange) return;
      this.curPage = 1;
      wx.showLoading({ mask: true, title: '商品更新' });
      let clearId = setTimeout(() => {
        clearTimeout(clearId);
        wx.hideLoading();
      }, 600);
      this.setData({ "goodsItems": [], allItemsLoaded: false, cellLoading: true }, () => {
        this.updataGoodItems(serverItems);
      });
    }).catch((errMsg) => {
      console.log(errMsg);    //错误提示信息
    });
  },
  updataGoodItems(newGoodItems) {
    let goodsItems = this.data.goodsItems;
    if (newGoodItems.length < this.data.pageNum) {         //如果不足一页
      goodsItems.push(...newGoodItems);
      this.setData({ "goodsItems": goodsItems, allItemsLoaded: true });
    } else {                                                 //如果满了一页
      this.setData({ cellLoading: false });
      this.setData({ curPage: this.data.curPage + 1 });
      let cutItems = newGoodItems.slice(0, this.data.pageNum);
      goodsItems.push(...cutItems);
      this.setData({ "goodsItems": goodsItems });
    }
  },
  //下拉刷新
  onPullDownRefresh() {
    //模拟加载
    let clearId = setTimeout(() => {
      clearTimeout(clearId);
      wx.stopPullDownRefresh();        //停止下拉刷新
    }, 800);
  },
  //加载更多
  onReachBottom() {
    // wx.pageScrollTo({scrollTop: 0,duration:0});
    console.log("触发加载更多....");
    if (this.data.cellLoading) return;
    this.setData({ cellLoading: true });
    utils.throttle(this.loadMore, 600, 300)();
  },
  loadMore() {
    this.requestData(false);
  },
  onLoad() {
    this.init();
    app.post('banner/getBanner', {
    }).then((res) => {
      let resultObj = res.data.data;
      this.setData({ topIcon: resultObj.bannerImage });
    }).catch((errMsg) => {
      console.log(errMsg);    //错误提示信息
    });
  },
  onShow(){
    if (app.globalData.authorization) {
      if (this.data.dialog){
        this.data.dialog.hideDialog();
        this.data.dialog=null;
      }
    }
    else if (!this.data.dialog){
      this.data.dialog = this.selectComponent("#dialog");
      wx.getSetting({
        success: res => {
          if (!res.authSetting['scope.userInfo']) {//未授权
            this.data.dialog.showDialog();
          } else {
            app.globalData.authorization = true;
          }
        }
      });
    }
    if (this.data.firstShow) {    //如果第一次加载数据
      this.data.firstShow = false;
      return;
    }
    //检查数据更新
    this.checkRefreshData();
  },
  //弹出框确认事件
  _confirmEvent() {
    app.globalData.authorization = true;
    this.data.dialog.hideDialog();
    this.data.dialog=null;
  },
  myCatchTouch(){

  },
  init() {
    //获取初始化信息
    this.requestData(true);
    //获取滚动条的高度
    wx.getSystemInfo({
      success: res => {
        this.setData({ scrollHeight: res.windowHeight });
      }
    });
  },
  openAGroup(e) {       //点击开团按钮事件处理
    console.log('e.currentTarget.dataset.gid=' + e.currentTarget.dataset.gid);
    let groupId = e.currentTarget.dataset.gid;
    wx.navigateTo({
      url: `../open/open?id=${groupId}`
    })
  }
})

